]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-group.c
Updated de translation.
[empathy.git] / libempathy / empathy-tp-group.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2006 Xavier Claessens <xclaesse@gmail.com>
4  * Copyright (C) 2007-2008 Collabora Ltd.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * Authors: Xavier Claessens <xclaesse@gmail.com>
21  */
22
23 #include <config.h>
24
25 #include <libmissioncontrol/mc-account.h>
26
27 #include <telepathy-glib/channel.h>
28 #include <telepathy-glib/util.h>
29 #include <telepathy-glib/interfaces.h>
30
31 #include "empathy-tp-group.h"
32 #include "empathy-contact-factory.h"
33 #include "empathy-utils.h"
34 #include "empathy-marshal.h"
35
36 #define DEBUG_FLAG EMPATHY_DEBUG_TP
37 #include "empathy-debug.h"
38
39 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpGroup)
40 typedef struct {
41         TpChannel             *channel;
42         gboolean               ready;
43
44         EmpathyContactFactory *factory;
45         McAccount             *account;
46         gchar                 *group_name;
47         guint                  self_handle;
48         GList                 *members;
49         GList                 *local_pendings;
50         GList                 *remote_pendings;
51 } EmpathyTpGroupPriv;
52
53 enum {
54         MEMBER_ADDED,
55         MEMBER_REMOVED,
56         LOCAL_PENDING,
57         REMOTE_PENDING,
58         DESTROY,
59         LAST_SIGNAL
60 };
61
62 enum {
63         PROP_0,
64         PROP_CHANNEL,
65         PROP_READY
66 };
67
68 static guint signals[LAST_SIGNAL];
69
70 G_DEFINE_TYPE (EmpathyTpGroup, empathy_tp_group, G_TYPE_OBJECT);
71
72 static EmpathyContact *
73 tp_group_get_contact (EmpathyTpGroup *group,
74                       guint           handle)
75 {
76         EmpathyTpGroupPriv *priv = GET_PRIV (group);
77         EmpathyContact     *contact = NULL;
78         
79         if (handle != 0) {
80                 contact = empathy_contact_factory_get_from_handle (priv->factory,
81                                                                    priv->account,
82                                                                    handle);
83         }
84
85         if (contact && handle == priv->self_handle) {
86                 empathy_contact_set_is_user (contact, TRUE);
87         }
88
89         return contact;
90 }
91
92 static GList *
93 tp_group_get_contacts (EmpathyTpGroup *group,
94                        const GArray   *handles)
95 {
96         EmpathyTpGroupPriv *priv = GET_PRIV (group);
97         GList              *contacts,  *l;
98
99         if (!handles) {
100                 return NULL;
101         }
102
103         contacts = empathy_contact_factory_get_from_handles (priv->factory,
104                                                              priv->account,
105                                                              handles);
106
107         /* FIXME: Only useful if the group has a different self handle than
108          * the connection, otherwise the contact factory already set that
109          * property. That can be known using group flags. */
110         for (l = contacts; l; l = l->next) {
111                 if (empathy_contact_get_handle (l->data) == priv->self_handle) {
112                         empathy_contact_set_is_user (l->data, TRUE);
113                 }
114         }
115
116         return contacts;
117 }
118
119 EmpathyPendingInfo *
120 empathy_pending_info_new (EmpathyContact *member,
121                           EmpathyContact *actor,
122                           const gchar    *message)
123 {
124         EmpathyPendingInfo *info;
125
126         info = g_slice_new0 (EmpathyPendingInfo);
127
128         if (member) {
129                 info->member = g_object_ref (member);
130         }
131         if (actor) {
132                 info->actor = g_object_ref (actor);
133         }
134         if (message) {
135                 info->message = g_strdup (message);
136         }
137
138         return info;
139 }
140
141 void
142 empathy_pending_info_free (EmpathyPendingInfo *info)
143 {
144         if (!info) {
145                 return;
146         }
147
148         if (info->member) {
149                 g_object_unref (info->member);
150         }
151         if (info->actor) {
152                 g_object_unref (info->actor);
153         }
154         g_free (info->message);
155
156         g_slice_free (EmpathyPendingInfo, info);
157 }
158
159 static gint
160 tp_group_local_pending_find (gconstpointer a,
161                              gconstpointer b)
162 {
163         const EmpathyPendingInfo *info = a;
164
165         return (info->member != b);
166 }
167
168 static void
169 tp_group_remove_from_pendings (EmpathyTpGroup *group,
170                                EmpathyContact *contact)
171 {
172         EmpathyTpGroupPriv *priv = GET_PRIV (group);
173         GList              *l;
174
175         /* local pending */
176         l = g_list_find_custom (priv->local_pendings,
177                                 contact,
178                                 tp_group_local_pending_find);
179         if (l) {
180                 empathy_pending_info_free (l->data);
181                 priv->local_pendings = g_list_delete_link (priv->local_pendings, l);
182         }
183
184         /* remote pending */
185         l = g_list_find (priv->remote_pendings, contact);
186         if (l) {
187                 g_object_unref (l->data);
188                 priv->remote_pendings = g_list_delete_link (priv->remote_pendings, l);
189         }
190 }
191
192 static void
193 tp_group_update_members (EmpathyTpGroup *group,
194                          const gchar    *message,
195                          const GArray   *added,
196                          const GArray   *removed,
197                          const GArray   *local_pending,
198                          const GArray   *remote_pending,
199                          guint           actor,
200                          guint           reason)
201 {
202         EmpathyTpGroupPriv *priv = GET_PRIV (group);
203         EmpathyContact     *actor_contact = NULL;
204         GList              *contacts, *l, *ll;
205
206         actor_contact = tp_group_get_contact (group, actor);
207
208         DEBUG ("Members changed for list %s:\n"
209                 "  added-len=%d, current-len=%d\n"
210                 "  removed-len=%d\n"
211                 "  local-pending-len=%d, current-len=%d\n"
212                 "  remote-pending-len=%d, current-len=%d",
213                 priv->group_name, added ? added->len : 0,
214                 g_list_length (priv->members), removed ? removed->len : 0,
215                 local_pending ? local_pending->len : 0,
216                 g_list_length (priv->local_pendings),
217                 remote_pending ? remote_pending->len : 0,
218                 g_list_length (priv->remote_pendings));
219
220         /* Contacts added */
221         contacts = tp_group_get_contacts (group, added);
222         for (l = contacts; l; l = l->next) {
223                 tp_group_remove_from_pendings (group, l->data);
224
225                 /* If the contact is not yet member, add it and emit signal */
226                 if (!g_list_find (priv->members, l->data)) {
227                         priv->members = g_list_prepend (priv->members,
228                                                         g_object_ref (l->data));
229                         g_signal_emit (group, signals[MEMBER_ADDED], 0,
230                                        l->data, actor_contact, reason, message);
231                 }
232                 g_object_unref (l->data);
233         }
234         g_list_free (contacts);
235
236         /* Contacts removed */
237         contacts = tp_group_get_contacts (group, removed);
238         for (l = contacts; l; l = l->next) {
239                 tp_group_remove_from_pendings (group, l->data);
240
241                 /* If the contact is member, remove it and emit signal */
242                 if ((ll = g_list_find (priv->members, l->data))) {
243                         g_object_unref (ll->data);
244                         priv->members = g_list_delete_link (priv->members, ll);
245                         g_signal_emit (group, signals[MEMBER_REMOVED], 0,
246                                        l->data, actor_contact, reason, message);
247                 }
248                 g_object_unref (l->data);
249         }
250         g_list_free (contacts);
251
252         /* Contacts local pending */
253         contacts = tp_group_get_contacts (group, local_pending);
254         for (l = contacts; l; l = l->next) {
255                 /* If the contact is not yet local-pending, add it and emit signal */
256                 if (!g_list_find_custom (priv->local_pendings, l->data,
257                                          tp_group_local_pending_find)) {
258                         EmpathyPendingInfo *info;
259
260                         info = empathy_pending_info_new (l->data,
261                                                          actor_contact,
262                                                          message);
263
264                         priv->local_pendings = g_list_prepend (priv->local_pendings, info);
265                         g_signal_emit (group, signals[LOCAL_PENDING], 0,
266                                        l->data, actor_contact, reason, message);
267                 }
268                 g_object_unref (l->data);
269         }
270         g_list_free (contacts);
271
272         /* Contacts remote pending */
273         contacts = tp_group_get_contacts (group, remote_pending);
274         for (l = contacts; l; l = l->next) {
275                 /* If the contact is not yet remote-pending, add it and emit signal */
276                 if (!g_list_find (priv->remote_pendings, l->data)) {
277                         priv->remote_pendings = g_list_prepend (priv->remote_pendings,
278                                                                 g_object_ref (l->data));
279                         g_signal_emit (group, signals[REMOTE_PENDING], 0,
280                                        l->data, actor_contact, reason, message);
281                 }
282                 g_object_unref (l->data);
283         }
284         g_list_free (contacts);
285
286         if (actor_contact) {
287                 g_object_unref (actor_contact);
288         }
289
290         DEBUG ("Members changed done for list %s:\n"
291                 "  members-len=%d\n"
292                 "  local-pendings-len=%d\n"
293                 "  remote-pendings-len=%d",
294                 priv->group_name, g_list_length (priv->members),
295                 g_list_length (priv->local_pendings),
296                 g_list_length (priv->remote_pendings));
297 }
298
299 static void
300 tp_group_members_changed_cb (TpChannel    *channel,
301                              const gchar  *message,
302                              const GArray *added,
303                              const GArray *removed,
304                              const GArray *local_pending,
305                              const GArray *remote_pending,
306                              guint         actor,
307                              guint         reason,
308                              gpointer      user_data,
309                              GObject      *group)
310 {
311         EmpathyTpGroupPriv *priv = GET_PRIV (group);
312
313         if (priv->ready) {
314                 tp_group_update_members (EMPATHY_TP_GROUP (group), message,
315                                          added, removed,
316                                          local_pending, remote_pending,
317                                          actor, reason);
318         }
319 }
320
321 static void
322 tp_group_get_members_cb (TpChannel    *channel,
323                          const GArray *handles,
324                          const GError *error,
325                          gpointer      user_data,
326                          GObject      *group)
327 {
328         EmpathyTpGroupPriv *priv = GET_PRIV (group);
329
330         if (error) {
331                 DEBUG ("Failed to get members: %s", error->message);
332                 return;
333         }
334
335         tp_group_update_members (EMPATHY_TP_GROUP (group),
336                                  NULL,    /* message */
337                                  handles, /* added */
338                                  NULL,    /* removed */
339                                  NULL,    /* local_pending */
340                                  NULL,    /* remote_pending */
341                                  0,       /* actor */
342                                  0);      /* reason */
343
344         DEBUG ("Ready");
345         priv->ready = TRUE;
346         g_object_notify (group, "ready");
347 }
348
349 static void
350 tp_group_get_local_pending_cb (TpChannel        *channel,
351                                const GPtrArray  *array,
352                                const GError     *error,
353                                gpointer          user_data,
354                                GObject          *group)
355 {
356         GArray *handles;
357         guint   i = 0;
358         
359         if (error) {
360                 DEBUG ("Failed to get local pendings: %s", error->message);
361                 return;
362         }
363
364         handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
365         g_array_append_val (handles, i);
366         for (i = 0; array->len > i; i++) {
367                 GValueArray *pending_struct;
368                 const gchar *message;
369                 guint        member_handle;
370                 guint        actor_handle;
371                 guint        reason;
372
373                 pending_struct = g_ptr_array_index (array, i);
374                 member_handle = g_value_get_uint (g_value_array_get_nth (pending_struct, 0));
375                 actor_handle = g_value_get_uint (g_value_array_get_nth (pending_struct, 1));
376                 reason = g_value_get_uint (g_value_array_get_nth (pending_struct, 2));
377                 message = g_value_get_string (g_value_array_get_nth (pending_struct, 3));
378
379                 g_array_index (handles, guint, 0) = member_handle;
380
381                 tp_group_update_members (EMPATHY_TP_GROUP (group),
382                                          message,      /* message */
383                                          NULL,         /* added */
384                                          NULL,         /* removed */
385                                          handles,      /* local_pending */
386                                          NULL,         /* remote_pending */
387                                          actor_handle, /* actor */
388                                          reason);      /* reason */
389         }
390         g_array_free (handles, TRUE);
391 }
392
393 static void
394 tp_group_get_remote_pending_cb (TpChannel    *channel,
395                                 const GArray *handles,
396                                 const GError *error,
397                                 gpointer      user_data,
398                                 GObject      *group)
399 {
400         if (error) {
401                 DEBUG ("Failed to get remote pendings: %s", error->message);
402                 return;
403         }
404
405         tp_group_update_members (EMPATHY_TP_GROUP (group),
406                                  NULL,    /* message */
407                                  NULL,    /* added */
408                                  NULL,    /* removed */
409                                  NULL,    /* local_pending */
410                                  handles, /* remote_pending */
411                                  0,       /* actor */
412                                  0);      /* reason */
413 }
414
415 static void
416 tp_group_inspect_handles_cb (TpConnection  *connection,
417                              const gchar  **names,
418                              const GError  *error,
419                              gpointer       user_data,
420                              GObject       *group)
421 {
422         EmpathyTpGroupPriv *priv = GET_PRIV (group);
423
424         if (error) {
425                 DEBUG ("Failed to inspect channel handle: %s", error->message);
426                 return;
427         }
428
429         priv->group_name = g_strdup (*names);
430 }
431
432 static void
433 tp_group_invalidated_cb (TpProxy        *proxy,
434                          guint           domain,
435                          gint            code,
436                          gchar          *message,
437                          EmpathyTpGroup *group)
438 {
439         DEBUG ("Channel invalidated: %s", message);
440         g_signal_emit (group, signals[DESTROY], 0);
441 }
442
443 static void
444 tp_group_get_self_handle_cb (TpChannel    *proxy,
445                              guint         handle,
446                              const GError *error,
447                              gpointer      user_data,
448                              GObject      *group)
449 {
450         EmpathyTpGroupPriv *priv = GET_PRIV (group);
451         TpConnection       *connection;
452         guint               channel_handle;
453         guint               channel_handle_type;
454         GArray             *handles;
455
456         if (error) {
457                 DEBUG ("Failed to get self handle: %s", error->message);
458                 return;
459         }
460
461         priv->self_handle = handle;
462         tp_cli_channel_interface_group_connect_to_members_changed (priv->channel,
463                                                                    tp_group_members_changed_cb,
464                                                                    NULL, NULL,
465                                                                    group, NULL);
466
467         /* GetMembers is called last, so it will be the last to get the reply,
468          * so we'll be ready once that call return. */
469         g_object_get (priv->channel,
470                       "connection", &connection,
471                       "handle-type", &channel_handle_type,
472                       "handle", &channel_handle,
473                       NULL);
474         handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), 1);
475         g_array_prepend_val (handles, channel_handle);
476         tp_cli_connection_call_inspect_handles (connection, -1,
477                                                 channel_handle_type,
478                                                 handles,
479                                                 tp_group_inspect_handles_cb,
480                                                 NULL, NULL,
481                                                 group);
482         g_array_free (handles, TRUE);
483
484         tp_cli_channel_interface_group_call_get_local_pending_members_with_info
485                                                         (priv->channel, -1,
486                                                          tp_group_get_local_pending_cb,
487                                                          NULL, NULL, 
488                                                          group);
489         tp_cli_channel_interface_group_call_get_remote_pending_members
490                                                         (priv->channel, -1,
491                                                          tp_group_get_remote_pending_cb,
492                                                          NULL, NULL, 
493                                                          group);
494         tp_cli_channel_interface_group_call_get_members (priv->channel, -1,
495                                                          tp_group_get_members_cb,
496                                                          NULL, NULL, 
497                                                          group);
498 }
499
500 static void
501 tp_group_factory_ready_cb (EmpathyTpGroup *group)
502 {
503         EmpathyTpGroupPriv      *priv = GET_PRIV (group);
504         EmpathyTpContactFactory *tp_factory;
505
506         tp_factory = empathy_contact_factory_get_tp_factory (priv->factory, priv->account);
507         g_signal_handlers_disconnect_by_func (tp_factory, tp_group_factory_ready_cb, group);
508         tp_cli_channel_interface_group_call_get_self_handle (priv->channel, -1,
509                                                              tp_group_get_self_handle_cb,
510                                                              NULL, NULL,
511                                                              G_OBJECT (group));
512 }
513
514 static void
515 tp_group_channel_ready_cb (EmpathyTpGroup *group)
516 {
517         EmpathyTpGroupPriv      *priv = GET_PRIV (group);
518         EmpathyTpContactFactory *tp_factory;
519
520         tp_factory = empathy_contact_factory_get_tp_factory (priv->factory,
521                                                              priv->account);
522         if (empathy_tp_contact_factory_is_ready (tp_factory)) {
523                 tp_group_factory_ready_cb (group);
524         } else {
525                 g_signal_connect_swapped (tp_factory, "notify::ready",
526                                           G_CALLBACK (tp_group_factory_ready_cb),
527                                           group);
528         }
529 }
530
531 static void
532 tp_group_finalize (GObject *object)
533 {
534         EmpathyTpGroupPriv      *priv = GET_PRIV (object);
535         EmpathyTpContactFactory *tp_factory;
536
537         DEBUG ("finalize: %p", object);
538
539         tp_factory = empathy_contact_factory_get_tp_factory (priv->factory, priv->account);
540         g_signal_handlers_disconnect_by_func (tp_factory, tp_group_factory_ready_cb, object);
541
542         if (priv->channel) {
543                 g_signal_handlers_disconnect_by_func (priv->channel,
544                                                       tp_group_invalidated_cb,
545                                                       object);
546                 g_object_unref (priv->channel);
547         }
548         if (priv->account) {
549                 g_object_unref (priv->account);
550         }
551         if (priv->factory) {
552                 g_object_unref (priv->factory);
553         }
554         g_free (priv->group_name);
555
556         g_list_foreach (priv->members, (GFunc) g_object_unref, NULL);
557         g_list_free (priv->members);
558
559         g_list_foreach (priv->local_pendings, (GFunc) empathy_pending_info_free, NULL);
560         g_list_free (priv->local_pendings);
561
562         g_list_foreach (priv->remote_pendings, (GFunc) g_object_unref, NULL);
563         g_list_free (priv->remote_pendings);
564
565         G_OBJECT_CLASS (empathy_tp_group_parent_class)->finalize (object);
566 }
567
568 static void
569 tp_group_constructed (GObject *group)
570 {
571         EmpathyTpGroupPriv *priv = GET_PRIV (group);
572         gboolean            channel_ready;
573
574         priv->factory = empathy_contact_factory_new ();
575         priv->account = empathy_channel_get_account (priv->channel);
576
577         g_signal_connect (priv->channel, "invalidated",
578                           G_CALLBACK (tp_group_invalidated_cb),
579                           group);
580
581         g_object_get (priv->channel, "channel-ready", &channel_ready, NULL);
582         if (channel_ready) {
583                 tp_group_channel_ready_cb (EMPATHY_TP_GROUP (group));
584         } else {
585                 g_signal_connect_swapped (priv->channel, "notify::channel-ready",
586                                           G_CALLBACK (tp_group_channel_ready_cb),
587                                           group);
588         }
589 }
590
591 static void
592 tp_group_get_property (GObject    *object,
593                        guint       param_id,
594                        GValue     *value,
595                        GParamSpec *pspec)
596 {
597         EmpathyTpGroupPriv *priv = GET_PRIV (object);
598
599         switch (param_id) {
600         case PROP_CHANNEL:
601                 g_value_set_object (value, priv->channel);
602                 break;
603         case PROP_READY:
604                 g_value_set_boolean (value, priv->ready);
605                 break;
606         default:
607                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
608                 break;
609         };
610 }
611
612 static void
613 tp_group_set_property (GObject      *object,
614                        guint         param_id,
615                        const GValue *value,
616                        GParamSpec   *pspec)
617 {
618         EmpathyTpGroupPriv *priv = GET_PRIV (object);
619
620         switch (param_id) {
621         case PROP_CHANNEL:
622                 priv->channel = g_object_ref (g_value_get_object (value));
623                 break;
624         default:
625                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
626                 break;
627         };
628 }
629
630 static void
631 empathy_tp_group_class_init (EmpathyTpGroupClass *klass)
632 {
633         GObjectClass *object_class = G_OBJECT_CLASS (klass);
634
635         object_class->finalize = tp_group_finalize;
636         object_class->constructed = tp_group_constructed;
637         object_class->get_property = tp_group_get_property;
638         object_class->set_property = tp_group_set_property;
639
640         g_object_class_install_property (object_class,
641                                          PROP_CHANNEL,
642                                          g_param_spec_object ("channel",
643                                                               "telepathy channel",
644                                                               "The channel for the group",
645                                                               TP_TYPE_CHANNEL,
646                                                               G_PARAM_READWRITE |
647                                                               G_PARAM_CONSTRUCT_ONLY));
648         g_object_class_install_property (object_class,
649                                          PROP_READY,
650                                          g_param_spec_boolean ("ready",
651                                                                "Is the object ready",
652                                                                "This object can't be used until this becomes true",
653                                                                FALSE,
654                                                                G_PARAM_READABLE));
655
656         signals[MEMBER_ADDED] =
657                 g_signal_new ("member-added",
658                               G_TYPE_FROM_CLASS (klass),
659                               G_SIGNAL_RUN_LAST,
660                               0,
661                               NULL, NULL,
662                               _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
663                               G_TYPE_NONE,
664                               4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
665
666         signals[MEMBER_REMOVED] =
667                 g_signal_new ("member-removed",
668                               G_TYPE_FROM_CLASS (klass),
669                               G_SIGNAL_RUN_LAST,
670                               0,
671                               NULL, NULL,
672                               _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
673                               G_TYPE_NONE,
674                               4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
675
676         signals[LOCAL_PENDING] =
677                 g_signal_new ("local-pending",
678                               G_TYPE_FROM_CLASS (klass),
679                               G_SIGNAL_RUN_LAST,
680                               0,
681                               NULL, NULL,
682                               _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
683                               G_TYPE_NONE,
684                               4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
685
686         signals[REMOTE_PENDING] =
687                 g_signal_new ("remote-pending",
688                               G_TYPE_FROM_CLASS (klass),
689                               G_SIGNAL_RUN_LAST,
690                               0,
691                               NULL, NULL,
692                               _empathy_marshal_VOID__OBJECT_OBJECT_UINT_STRING,
693                               G_TYPE_NONE,
694                               4, EMPATHY_TYPE_CONTACT, EMPATHY_TYPE_CONTACT, G_TYPE_UINT, G_TYPE_STRING);
695
696         signals[DESTROY] =
697                 g_signal_new ("destroy",
698                               G_TYPE_FROM_CLASS (klass),
699                               G_SIGNAL_RUN_LAST,
700                               0,
701                               NULL, NULL,
702                               g_cclosure_marshal_VOID__VOID,
703                               G_TYPE_NONE,
704                               0);
705
706         g_type_class_add_private (object_class, sizeof (EmpathyTpGroupPriv));
707 }
708
709 static void
710 empathy_tp_group_init (EmpathyTpGroup *group)
711 {
712         EmpathyTpGroupPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (group,
713                 EMPATHY_TYPE_TP_GROUP, EmpathyTpGroupPriv);
714
715         group->priv = priv;
716 }
717
718 EmpathyTpGroup *
719 empathy_tp_group_new (TpChannel *channel)
720 {
721         g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
722
723         return g_object_new (EMPATHY_TYPE_TP_GROUP, 
724                              "channel", channel,
725                              NULL);
726 }
727
728 static void
729 tp_group_async_cb (TpChannel    *channel,
730                    const GError *error,
731                    gpointer      user_data,
732                    GObject      *weak_object)
733 {
734         if (error) {
735                 DEBUG ("%s: %s", (gchar*) user_data, error->message);
736         }
737 }
738
739 void
740 empathy_tp_group_close (EmpathyTpGroup *group)
741 {
742         EmpathyTpGroupPriv *priv = GET_PRIV (group);
743
744         g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
745         g_return_if_fail (priv->ready);
746
747         tp_cli_channel_call_close (priv->channel, -1,
748                                    tp_group_async_cb,
749                                    "Failed to close", NULL,
750                                    G_OBJECT (group));
751 }
752
753 static GArray *
754 tp_group_get_handles (GList *contacts)
755 {
756         GArray *handles;
757         guint   length;
758         GList  *l;
759
760         length = g_list_length (contacts);
761         handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), length);
762
763         for (l = contacts; l; l = l->next) {
764                 guint handle;
765
766                 handle = empathy_contact_get_handle (l->data);
767                 g_array_append_val (handles, handle);
768         }
769
770         return handles;
771 }
772
773 void
774 empathy_tp_group_add_members (EmpathyTpGroup *group,
775                               GList          *contacts,
776                               const gchar    *message)
777 {
778         EmpathyTpGroupPriv *priv = GET_PRIV (group);
779         GArray             *handles;
780
781         g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
782         g_return_if_fail (contacts != NULL);
783         g_return_if_fail (priv->ready);
784
785         handles = tp_group_get_handles (contacts);
786         tp_cli_channel_interface_group_call_add_members (priv->channel, -1,
787                                                          handles,
788                                                          message,
789                                                          tp_group_async_cb,
790                                                          "Failed to add members", NULL,
791                                                          G_OBJECT (group));
792         g_array_free (handles, TRUE);
793 }
794
795 void
796 empathy_tp_group_remove_members (EmpathyTpGroup *group,
797                                  GList          *contacts,
798                                  const gchar    *message)
799 {
800         EmpathyTpGroupPriv *priv = GET_PRIV (group);
801         GArray             *handles;
802
803         g_return_if_fail (EMPATHY_IS_TP_GROUP (group));
804         g_return_if_fail (contacts != NULL);
805         g_return_if_fail (priv->ready);
806
807         handles = tp_group_get_handles (contacts);
808         tp_cli_channel_interface_group_call_remove_members (priv->channel, -1,
809                                                             handles,
810                                                             message,
811                                                             tp_group_async_cb,
812                                                             "Failed to remove members", NULL,
813                                                             G_OBJECT (group));
814         g_array_free (handles, TRUE);
815 }
816
817 void
818 empathy_tp_group_add_member (EmpathyTpGroup *group,
819                              EmpathyContact *contact,
820                              const gchar    *message)
821 {
822         GList *contacts;
823
824         contacts = g_list_prepend (NULL, contact);
825         empathy_tp_group_add_members (group, contacts, message);
826         g_list_free (contacts);
827 }
828
829 void
830 empathy_tp_group_remove_member (EmpathyTpGroup *group,
831                                 EmpathyContact *contact,
832                                 const gchar    *message)
833 {
834         GList *contacts;
835
836         contacts = g_list_prepend (NULL, contact);
837         empathy_tp_group_remove_members (group, contacts, message);
838         g_list_free (contacts);
839 }
840
841 GList *
842 empathy_tp_group_get_members (EmpathyTpGroup *group)
843 {
844         EmpathyTpGroupPriv *priv = GET_PRIV (group);
845
846         g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
847
848         g_list_foreach (priv->members, (GFunc) g_object_ref, NULL);
849
850         return g_list_copy (priv->members);
851 }
852
853 GList *
854 empathy_tp_group_get_local_pendings (EmpathyTpGroup *group)
855 {
856         EmpathyTpGroupPriv *priv = GET_PRIV (group);
857         GList              *pendings = NULL, *l;
858
859         g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
860
861         for (l = priv->local_pendings; l; l = l->next) {
862                 EmpathyPendingInfo *info;
863                 EmpathyPendingInfo *new_info;
864
865                 info = l->data;
866                 new_info = empathy_pending_info_new (info->member,
867                                                      info->actor,
868                                                      info->message);
869                 pendings = g_list_prepend (pendings, new_info);
870         }
871
872         return pendings;
873 }
874
875 GList *
876 empathy_tp_group_get_remote_pendings (EmpathyTpGroup *group)
877 {
878         EmpathyTpGroupPriv *priv = GET_PRIV (group);
879
880         g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
881
882         g_list_foreach (priv->remote_pendings, (GFunc) g_object_ref, NULL);
883
884         return g_list_copy (priv->remote_pendings);
885 }
886
887 const gchar *
888 empathy_tp_group_get_name (EmpathyTpGroup *group)
889 {
890         EmpathyTpGroupPriv *priv = GET_PRIV (group);
891
892         g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
893         g_return_val_if_fail (priv->ready, NULL);
894
895         return priv->group_name;
896 }
897
898 EmpathyContact *
899 empathy_tp_group_get_self_contact (EmpathyTpGroup *group)
900 {
901         EmpathyTpGroupPriv *priv = GET_PRIV (group);
902
903         g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), NULL);
904         g_return_val_if_fail (priv->ready, NULL);
905
906         return tp_group_get_contact (group, priv->self_handle);
907 }
908
909 gboolean
910 empathy_tp_group_is_member (EmpathyTpGroup *group,
911                             EmpathyContact *contact)
912 {
913         EmpathyTpGroupPriv *priv = GET_PRIV (group);
914
915         g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), FALSE);
916         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
917
918         return g_list_find (priv->members, contact) != NULL;
919 }
920
921 gboolean
922 empathy_tp_group_is_ready (EmpathyTpGroup *group)
923 {
924         EmpathyTpGroupPriv *priv = GET_PRIV (group);
925
926         g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), FALSE);
927
928         return priv->ready;
929 }
930
931 EmpathyPendingInfo *
932 empathy_tp_group_get_invitation (EmpathyTpGroup  *group,
933                                  EmpathyContact **remote_contact)
934 {
935         EmpathyTpGroupPriv *priv = GET_PRIV (group);
936         EmpathyContact     *contact = NULL;
937         EmpathyPendingInfo *invitation = NULL;
938         GList              *l;
939
940         g_return_val_if_fail (EMPATHY_IS_TP_GROUP (group), FALSE);
941         g_return_val_if_fail (priv->ready, NULL);
942
943         for (l = priv->local_pendings; l; l = l->next) {
944                 EmpathyPendingInfo *info = l->data;
945
946                 if (empathy_contact_is_user (info->member)) {
947                         invitation = info;
948                         break;
949                 }
950         }
951
952         if (invitation) {
953                 contact = invitation->actor;
954         }
955         if (!invitation) {
956                 if (priv->remote_pendings) {
957                         contact = priv->remote_pendings->data;
958                 }
959                 else if (priv->members) {
960                         contact = priv->members->data;
961                 }
962         }
963
964         if (remote_contact && contact) {
965                 *remote_contact = g_object_ref (contact);
966         }
967
968         return invitation;
969 }
970