]> git.0d.be Git - empathy.git/blob - libempathy/empathy-individual-manager.c
Base the contact list around libfolks metacontacts. Not yet to feature-parity
[empathy.git] / libempathy / empathy-individual-manager.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2010 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  *          Travis Reitter <travis.reitter@collabora.co.uk>
21  */
22
23 #include <config.h>
24
25 #include <string.h>
26
27 #include <telepathy-glib/account-manager.h>
28 #include <telepathy-glib/enums.h>
29 #include <telepathy-glib/proxy-subclass.h>
30 #include <telepathy-glib/util.h>
31
32 #include <folks/folks.h>
33
34 #include <extensions/extensions.h>
35
36 #include "empathy-individual-manager.h"
37 #include "empathy-contact-manager.h"
38 #include "empathy-contact-list.h"
39 #include "empathy-marshal.h"
40 #include "empathy-utils.h"
41
42 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
43 #include "empathy-debug.h"
44
45 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIndividualManager)
46 typedef struct
47 {
48   FolksIndividualAggregator *aggregator;
49   EmpathyContactManager *contact_manager;
50   TpProxy *logger;
51   /* account object path (gchar *) => GHashTable containing favorite contacts
52    * (contact ID (gchar *) => TRUE) */
53   GHashTable *favourites;
54   TpProxySignalConnection *favourite_contacts_changed_signal;
55 } EmpathyIndividualManagerPriv;
56
57 G_DEFINE_TYPE (EmpathyIndividualManager, empathy_individual_manager,
58     G_TYPE_OBJECT);
59
60 static EmpathyIndividualManager *manager_singleton = NULL;
61
62 static void
63 individual_group_changed_cb (FolksIndividual *individual,
64     gchar *group,
65     gboolean is_member,
66     EmpathyIndividualManager *self)
67 {
68   g_signal_emit_by_name (self, "groups-changed", individual, group,
69       is_member);
70 }
71
72 static void
73 aggregator_individuals_added_cb (FolksIndividualAggregator *aggregator,
74     GList *individuals,
75     EmpathyIndividualManager *self)
76 {
77   GList *l;
78
79   for (l = individuals; l; l = l->next)
80     {
81       g_signal_connect (l->data, "group-changed",
82           G_CALLBACK (individual_group_changed_cb), self);
83     }
84
85   /* TODO: don't hard-code the reason or message */
86   g_signal_emit_by_name (self, "members-changed",
87       "individual(s) added", individuals, NULL,
88       TP_CHANNEL_GROUP_CHANGE_REASON_NONE, TRUE);
89 }
90
91 static void
92 aggregator_individuals_removed_cb (FolksIndividualAggregator *aggregator,
93     GList *individuals,
94     EmpathyIndividualManager *self)
95 {
96   GList *l;
97
98   for (l = individuals; l; l = l->next)
99     {
100       g_signal_handlers_disconnect_by_func (l->data,
101           individual_group_changed_cb, self);
102     }
103
104   /* TODO: don't hard-code the reason or message */
105   g_signal_emit_by_name (self, "members-changed",
106       "individual(s) removed", NULL, individuals,
107       TP_CHANNEL_GROUP_CHANGE_REASON_NONE, TRUE);
108 }
109
110 static void
111 individual_manager_finalize (GObject *object)
112 {
113   EmpathyIndividualManagerPriv *priv = GET_PRIV (object);
114
115   tp_proxy_signal_connection_disconnect (
116       priv->favourite_contacts_changed_signal);
117
118   if (priv->logger != NULL)
119     g_object_unref (priv->logger);
120
121   if (priv->contact_manager != NULL)
122     g_object_unref (priv->contact_manager);
123
124   if (priv->aggregator != NULL)
125     g_object_unref (priv->aggregator);
126
127   g_hash_table_destroy (priv->favourites);
128 }
129
130 static GObject *
131 individual_manager_constructor (GType type,
132     guint n_props,
133     GObjectConstructParam *props)
134 {
135   GObject *retval;
136
137   if (manager_singleton)
138     {
139       retval = g_object_ref (manager_singleton);
140     }
141   else
142     {
143       retval =
144           G_OBJECT_CLASS (empathy_individual_manager_parent_class)->
145           constructor (type, n_props, props);
146
147       manager_singleton = EMPATHY_INDIVIDUAL_MANAGER (retval);
148       g_object_add_weak_pointer (retval, (gpointer) & manager_singleton);
149     }
150
151   return retval;
152 }
153
154 /**
155  * empathy_individual_manager_initialized:
156  *
157  * Reports whether or not the singleton has already been created.
158  *
159  * There can be instances where you want to access the #EmpathyIndividualManager
160  * only if it has been set up for this process.
161  *
162  * Returns: %TRUE if the #EmpathyIndividualManager singleton has previously
163  * been initialized.
164  */
165 gboolean
166 empathy_individual_manager_initialized (void)
167 {
168   return (manager_singleton != NULL);
169 }
170
171 static void
172 empathy_individual_manager_class_init (EmpathyIndividualManagerClass *klass)
173 {
174   GObjectClass *object_class = G_OBJECT_CLASS (klass);
175
176   object_class->finalize = individual_manager_finalize;
177   object_class->constructor = individual_manager_constructor;
178
179   g_signal_new ("groups-changed",
180       G_TYPE_FROM_CLASS (klass),
181       G_SIGNAL_RUN_LAST,
182       0,
183       NULL, NULL,
184       _empathy_marshal_VOID__OBJECT_STRING_BOOLEAN,
185       G_TYPE_NONE, 3, FOLKS_TYPE_INDIVIDUAL, G_TYPE_STRING, G_TYPE_BOOLEAN);
186
187   g_signal_new ("members-changed",
188       G_TYPE_FROM_CLASS (klass),
189       G_SIGNAL_RUN_LAST,
190       0,
191       NULL, NULL,
192       _empathy_marshal_VOID__STRING_OBJECT_OBJECT_UINT,
193       G_TYPE_NONE,
194       4, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_UINT);
195
196   g_type_class_add_private (object_class,
197       sizeof (EmpathyIndividualManagerPriv));
198 }
199
200 static void
201 empathy_individual_manager_init (EmpathyIndividualManager *self)
202 {
203   EmpathyIndividualManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
204       EMPATHY_TYPE_INDIVIDUAL_MANAGER, EmpathyIndividualManagerPriv);
205   TpDBusDaemon *bus;
206   GError *error = NULL;
207
208   self->priv = priv;
209   priv->contact_manager = empathy_contact_manager_dup_singleton ();
210
211   priv->favourites = g_hash_table_new_full (g_str_hash, g_str_equal,
212       (GDestroyNotify) g_free, (GDestroyNotify) g_hash_table_unref);
213
214   priv->aggregator = folks_individual_aggregator_new ();
215   if (error == NULL)
216     {
217       g_signal_connect (priv->aggregator, "individuals-added",
218           G_CALLBACK (aggregator_individuals_added_cb), self);
219       g_signal_connect (priv->aggregator, "individuals-removed",
220           G_CALLBACK (aggregator_individuals_removed_cb), self);
221     }
222   else
223     {
224       DEBUG ("Failed to get individual aggregator: %s", error->message);
225       g_clear_error (&error);
226     }
227
228   bus = tp_dbus_daemon_dup (&error);
229
230   if (error == NULL)
231     {
232       priv->logger = g_object_new (TP_TYPE_PROXY,
233           "bus-name", "org.freedesktop.Telepathy.Logger",
234           "object-path",
235           "/org/freedesktop/Telepathy/Logger", "dbus-daemon", bus, NULL);
236       g_object_unref (bus);
237
238       tp_proxy_add_interface_by_id (priv->logger, EMP_IFACE_QUARK_LOGGER);
239     }
240   else
241     {
242       DEBUG ("Failed to get telepathy-logger proxy: %s", error->message);
243       g_clear_error (&error);
244     }
245 }
246
247 EmpathyIndividualManager *
248 empathy_individual_manager_dup_singleton (void)
249 {
250   return g_object_new (EMPATHY_TYPE_INDIVIDUAL_MANAGER, NULL);
251 }
252
253 /* TODO: support adding and removing Individuals */
254
255 GList *
256 empathy_individual_manager_get_members (EmpathyIndividualManager *self)
257 {
258   EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
259   GHashTable *individuals;
260
261   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self), NULL);
262
263   individuals = folks_individual_aggregator_get_individuals (priv->aggregator);
264   return individuals ? g_hash_table_get_values (individuals) : NULL;
265 }
266
267 FolksIndividual *
268 empathy_individual_manager_lookup_member (EmpathyIndividualManager *self,
269     const gchar *id)
270 {
271   EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
272   GHashTable *individuals;
273
274   g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self), NULL);
275
276   individuals = folks_individual_aggregator_get_individuals (priv->aggregator);
277   if (individuals != NULL)
278     return g_hash_table_lookup (individuals, id);
279
280   return NULL;
281 }
282
283 void
284 empathy_individual_manager_remove (EmpathyIndividualManager *self,
285     FolksIndividual *individual,
286     const gchar *message)
287 {
288   /* TODO: implement */
289   DEBUG (G_STRLOC ": individual removal not implemented");
290 }